package main

import kotlin.system.exitProcess

fun main(args: Array<String>) {
    val list: IntArray = intArrayOf(1, 2, 3)
    val strList: Array<String> = arrayOf("2", "2hihd", "m", "e", "n", "t")

    strList.forEach(::println)
    println(list.joinToString(""))
    println(strList.sliceArray(1..4).joinToString(""))

//    checkArgs(args)
//
//    val arg1=args[0].toInt()
//    val arg2=args[1].toInt()
//    println("$arg1+$arg2=${sum(arg1,arg2)}")

    println(int2Long(3))

    println(multiply(4, 5))
    println(multiply.invoke(3, 5))

    val person = Star("周杰伦", 40)
    person.selfIntroduction()

    Outer@ for (i in 0..10) {
        var j = i
        while (j >= 0) {
            print("$j,")
            if (j == 5) continue@Outer
            if (j == 7) break@Outer
            j--
        }
    }

    printX(1, 1, 2, 3, string = "年后")

    val personalDriver = PersonalDriver()
    val pptWriter = PPTWriter()
    val manager = Manager(personalDriver, pptWriter)
    manager.write()
    manager.drive()
}

val int2Long = fun(x: Int): Double = x.toDouble()

//function
fun checkArgs(args: Array<String>) {
    if (args.size < 2) {
        printUseHelp()
        exitProcess(0)
    }
}

fun printUseHelp() {
    println("请检查是否输入两个整数参数")
}

fun sum(arg1: Int, arg2: Int): Int = arg1 + arg2

//lambda表达式
val multiply = { arg1: Int, arg2: Int ->
    println("$arg1*$arg2=${arg1 * arg2}")
    arg1 * arg2
}

//接口、抽象
interface Communicate {
    fun selfIntroduction()
}

abstract class Person(val name: String, open var age: Int) : Communicate {
    override fun selfIntroduction() {
        println("大家好，我叫$name,年龄$age")
    }
}

class Star(name: String, age: Int) : Person(name, age) {
    override var age: Int
        get() = super.age
        set(value) {}
}

//变长参数
fun printX(vararg args: Int, string: String) {
    args.forEach(::println)
    println(string)
}


//接口代理
interface Driver {
    fun drive()
}

interface Writer {
    fun write()
}

class PersonalDriver : Driver {
    override fun drive() {
        println("私人司机正在开车")
    }
}

class PPTWriter : Writer {
    override fun write() {
        println("正在制作PPT")
    }
}

class Manager(driver: Driver, writer: Writer) : Driver by driver, Writer by writer

//复合函数
infix fun <P1, P2, R> Function1<P1, P2>.andThen(function: Function1<P2, R>): Function1<P1, R> {
    return fun(p1: P1): R {
        return function.invoke(this.invoke(p1))
    }
}

infix fun <P1, P2, R> Function1<P2, R>.compose(function: Function1<P1, P2>): Function1<P1, R> {
    return fun(p1: P1): R {
        return this.invoke(function.invoke(p1))
    }
}